package com.zhuiyun.pm.client.home.utils;

import com.zhuiyun.pm.client.action.entity.GoodsEntity;
import com.zhuiyun.pm.client.action.mapper.GoodsMapper;
import com.zhuiyun.pm.client.home.mapper.AnnouncementMapper;
import com.zhuiyun.pm.client.home.pojo.ClientAnnouncement;
import com.zhuiyun.pm.utils.RedisKeyExpirationListener;
import com.zhuiyun.pm.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;


/**
 * @ClassName RedisKeyExpire
 * @Description TODO 监听key过期
 * @Author HH
 * @Date 2023/3/14 19:10
 **/
@Component
@Transactional
public class RedisKeyExpire extends RedisKeyExpirationListener {

    @Autowired
    AnnouncementMapper announcementMapper;

    @Autowired
    RedisUtil redisUtil;

    String key = "announcementPageViews:announcementPageViews";
    String key2 = "announcementPageViews_2:announcementPageViews";

    public RedisKeyExpire(RedisMessageListenerContainer listenerContainer) {
        super(listenerContainer);
    }

    /**
     * 监听redis过期key
     * @param message
     * @param pattern
     */
    @Override
    public void onMessage(Message message, byte[] pattern) {
        if(message.toString().substring(0,43).equals(key)){
            String id = message.toString().substring(43);
            ClientAnnouncement clientAnnouncement = new ClientAnnouncement();
            clientAnnouncement.setId(Integer.valueOf(id));
            clientAnnouncement.setPageViews((Integer) redisUtil.get(key2 + id));
            announcementMapper.updateByPrimaryKeySelective(clientAnnouncement);
            redisUtil.del(key2 + id);
        }


    }
}
